//
// Created by v on 19-6-27.
//

#include "CMHttpExCurl.h"

CMHttpExCurl::CMHttpExCurl() {}

CMHttpExCurl::~CMHttpExCurl() {}

size_t curl_write(void *buffer, size_t size, size_t nmemb, void *userp)
{
    CMHttpExSetFunction *f = (CMHttpExSetFunction *)userp;
    return f->write_data(buffer, size, nmemb, userp);
}

int CMHttpExCurl::getSecure(const char *url, const char *caPath, int ctimeout,
                            int timeout)
{
#ifndef USE_LIBCURL
    return -101;
#else
    CURLcode res;
    CURL *   curl = curl_easy_init();
    if (NULL == curl)
    {
        return -CURLE_FAILED_INIT;
    }

    if (m_debug)
    {
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    }

    struct curl_slist *headers = NULL;
    if (!m_header.empty())
    {
        std::map<std::string, std::string>::iterator iter;
        for (iter = m_header.begin(); iter != m_header.end(); iter++)
        {
            std::string h = iter->first;
            h.append(": ");
            h.append(iter->second);
            headers = curl_slist_append(headers, h.c_str());
        }
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    }
    if (!m_data.empty())
    {
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, m_data.c_str());
    }

    FILE *f = NULL;
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
    if (m_function)
    {
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, m_function);
    }
    else
    {
        if (m_savefile.length() > 0)
        {
            f = fopen(m_savefile.c_str(), "w");
            if (f)
            {
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)f);
            }
        }
    }
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    if (NULL == caPath)
    {
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,
                         false); //设定为不验证证书和HOST
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    else
    {
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_easy_setopt(curl, CURLOPT_CAINFO, caPath);
    }
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, ctimeout);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
    res = curl_easy_perform(curl);
    if (headers)
    {
        curl_slist_free_all(headers);
    }
    curl_easy_cleanup(curl);
    if (f)
        fclose(f);
    return res != CURLE_OK ? -res : 0;
#endif
}

int CMHttpExCurl::get(const char *url, int ctimeout, int timeout)
{
#ifndef USE_LIBCURL
    return -101;
#else
    CURLcode res;
    CURL *   curl = curl_easy_init();
    if (NULL == curl)
    {
        return -CURLE_FAILED_INIT;
    }

    if (m_debug)
    {
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    }

    FILE *f = NULL;
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
    if (m_function)
    {
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, m_function);
    }
    else
    {
        if (m_savefile.length() > 0)
        {
            f = fopen(m_savefile.c_str(), "w");
            if (f)
            {
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)f);
            }
        }
    }
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, ctimeout);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    if (f)
        fclose(f);
    return res != CURLE_OK ? -res : 0;
#endif
    return 0;
}
